<template>
  <div class="edit-menu">
    <a-modal
      title="新增子参数"
      width="900px"
      :visible="visibles"
      :confirm-loading="confirmLoading"
      :footer="null"
      @cancel="handleCancel"
    >
      <s-table
        ref="table"
        size="default"
        :rowKey="(record) => record.ID"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <div slot="tabAction">
          <a-button type="primary" icon="plus" @click="handleOperation('add')">新增</a-button>
        </div>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="up" slot-scope="record">
          <a-tag @click="handleUp(record.ID)"><a-icon type="arrow-up" /></a-tag>
        </span>
        <span slot="down" slot-scope="record">
          <a-tag @click="handelDown(record.ID)"><a-icon type="arrow-down" /></a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleOperation('update', record.ID)">修改</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="您确定是否删除当前参数?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.ID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
      <a-modal
        title="新增"
        width="600px"
        :visible="Addvisible"
        :confirm-loading="confirmLoading"
        @ok="handleSubmit"
        @cancel="handleAddCancel"
      >
        <a-spin :spinning="requestSpinning" tip="正在加载数据中...">
          <a-form :form="form" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
            <a-form-item label="键名">
              <a-input
                v-decorator="[
                  'PmName',
                  {
                    rules: [{ required: true, message: '请输入键名', whitespace: true, trigger: 'change' }],
                  },
                ]"
                autocomplete="off"
                placeholder="请输入键名"
              />
            </a-form-item>
            <a-form-item label="键值">
              <a-input v-decorator="['PmKey', { initialValue: '' }]" autocomplete="off" placeholder="请输入键值" />
            </a-form-item>
            <a-form-item label="颜色" v-show="pid === '503'">
              <a-input
                type="color"
                v-decorator="['PmColor', { initialValue: '' }]"
                autocomplete="off"
                placeholder="选择颜色"
              />
            </a-form-item>
            <a-form-item label="状态">
              <a-switch
                v-decorator="['PmEnable', { valuePropName: 'checked', initialValue: true }]"
                checked-children="启用"
                un-checked-children="停用"
              />
            </a-form-item>
            <a-form-item label="排序值">
              <a-input-number v-decorator="['PmSort', { initialValue: '' }]" :min="0" placeholder="排序值" />
            </a-form-item>
            <a-form-item label="备注">
              <a-textarea v-decorator="['PmBreak', { initialValue: '' }]" placeholder="请输入备注"></a-textarea>
            </a-form-item>
          </a-form>
        </a-spin>
      </a-modal>
    </a-modal>
  </div>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getParameterList, getParameterInfo, setParameters } from '@/api/common'
const columns = [
  {
    title: '序号',
    align: 'center',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '参数键名',
    align: 'center',
    dataIndex: 'PmName'
  },
  {
    title: '参数键值',
    align: 'center',
    dataIndex: 'PmKey'
  },
  {
    title: '排序',
    align: 'left',
    dataIndex: 'PmSort'
  },
  {
    title: '状态',
    align: 'center',
    dataIndex: 'PmEnable',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '上移',
    align: 'left',
    width: '80px',
    scopedSlots: { customRender: 'up' }
  },
  {
    title: '下移',
    align: 'left',
    width: '80px',
    scopedSlots: { customRender: 'down' }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]
const statusMap = {
  0: {
    status: 'default',
    text: '停用'
  },
  1: {
    status: 'processing',
    text: '启用'
  }
}

export default {
  props: {
    visibles: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    }
  },
  components: {
    STable,
    Ellipsis
  },
  data () {
    this.columns = columns
    return {
      confirmLoading: false,
      Addvisible: false,
      requestSpinning: false,
      form: this.$form.createForm(this, { name: 'parameterChild' }),
      propsData: {
        types: 'add',
        id: ''
      },
      queryParam: {
        mode: '1',
        pid: this.pid,
        strContent: {
          Pmkey: '',
          PmName: ''
        }
      },
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        const index = this.columns.findIndex((e) => e.dataIndex === 'PmColor')
        if (this.pid === '503') {
          if (index === -1) {
            this.columns.splice(2, 0, {
              title: '颜色',
              align: 'center',
              dataIndex: 'PmColor',
              customRender: (text) => <div style={{ 'background-color': text }} class="color-block"></div>
            })
          }
        } else {
          if (index !== -1) {
            this.columns.splice(index, 1)
          }
        }
        return getParameterList(requestParameters).then((res) => {
          return res
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    fetchData () {
      const {
        propsData: { id: ParameterID },
        form: { setFieldsValue },
        $message
      } = this
      this.requestSpinning = true
      getParameterInfo({ ParameterID })
        .then((res) => {
          this.$nextTick(function () {
            setFieldsValue({
              PmKey: res.data.PmKey,
              PmName: res.data.PmName,
              PmBreak: res.data.PmBreak,
              PmSort: res.data.PmSort,
              PmEnable: res.data.PmEnable === '1',
              PmColor: res.data.PmColor
            })
          })
        })
        .catch((err) => {
          $message.error(err)
        })
        .finally(() => {
          this.requestSpinning = false
        })
    },
    handleOperation (types, id) {
      this.Addvisible = true
      this.propsData = { types, id }
      if (types === 'update') {
        this.fetchData()
      }
    },
    handleSubmit () {
      const {
        form: { validateFields },
        pid: PID,
        propsData: { types, id: ID }
      } = this
      validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          setParameters({
            State: types,
            sys_parameter_Modules: Object.assign({}, values, {
              ID,
              PID,
              PmEnable: values.PmEnable ? '1' : '0',
              CreateTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
              CreateUserID: this.$store.getters.userid,
              CreateUserName: this.$store.getters.name,
              Mode: 0,
              PmColor: this.pid === '503' ? values.PmColor : ''
            })
          })
            .then((res) => {
              if (res.code === 200) {
                this.$message.success('提交成功', 3)
                this.handleAddCancel()
                this.$refs.table.refresh(true)
              } else {
                this.$message.error(res.msg, 3)
              }
            })
            .catch(() => {
              this.$message.error('提交失败', 3)
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleAddCancel () {
      this.Addvisible = false
      this.form.resetFields()
    },
    handleUp (ID) {
      setParameters({ State: 'up', sys_parameter_Modules: { ID } })
        .then((res) => {
          if (res.code === 200) {
            this.$message.success('参数上移成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('参数上移失败')
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    handelDown (ID) {
      setParameters({ State: 'down', sys_parameter_Modules: { ID } })
        .then((res) => {
          if (res.code === 200) {
            this.$message.success('参数下移成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('参数下移失败')
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    handleRemove (ID) {
      setParameters({
        State: 'delete',
        sys_parameter_Modules: { ID }
      })
        .then((res) => {
          if (res.code === 200) {
            this.$refs.table.refresh(true)
            this.$message.success('参数删除成功', 2.5)
          } else {
            this.$message.error('参数删除失败, 请稍后重试', 2.5)
          }
        })
        .catch((err) => {
          this.$message.error(err, 2.5)
        })
    },
    handleCancel () {
      this.$emit('update:visibles', false)
    }
  }
}
</script>
<style lang="less" scoped>
.color-block {
  width: 20px;
  height: 20px;
  border: 1px solid #e8e8e8;
  background-color: #fff;
  margin: 0 auto;
  border-radius: 4px;
}
</style>
